//
//  KAEMediaPickerController.m
//  StudyDemo
//
//  Created by 陶涛 on 2018/8/7.
//  Copyright © 2018年 陶涛. All rights reserved.
//

#import "KAEMediaPickerController.h"
#import <MediaPlayer/MediaPlayer.h>

@interface KAEMediaPickerController ()<MPMediaPickerControllerDelegate>

@property (nonatomic, copy) NSArray<MPMediaItem *> *mediaItems;

@property (nonatomic, strong) MPMusicPlayerController *musicPlayer;

@end

static NSString *const kCellReuseIdentifier = @"kCellReuseIdentifier";

@implementation KAEMediaPickerController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"选择音乐";
    
    [self setUI];
}

- (MPMusicPlayerController *)musicPlayer {
    if (!_musicPlayer) {
        _musicPlayer = [MPMusicPlayerController systemMusicPlayer];
    }
    return _musicPlayer;
}

- (void)setUI {
    UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithTitle:@"选择" style:UIBarButtonItemStylePlain target:self action:@selector(chooseMusic:)];
    self.navigationItem.rightBarButtonItem = btn;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kCellReuseIdentifier];
}

- (void)chooseMusic:(UIBarButtonItem *)sender {
    MPMediaPickerController *mediaPickController = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeAnyAudio];
    mediaPickController.delegate = self;
    mediaPickController.prompt = @"请选择你喜欢的音乐";
    mediaPickController.allowsPickingMultipleItems = YES;
    mediaPickController.showsCloudItems = YES;
    [self presentViewController:mediaPickController animated:YES completion:NULL];
}

- (NSArray<MPMediaItem *> *)mediaItems
{
    if (!_mediaItems) {
        _mediaItems = [NSArray array];
    }
    return _mediaItems;
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.mediaItems.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellReuseIdentifier];
    MPMediaItem *item = self.mediaItems[indexPath.row];
    cell.textLabel.text = item.title;
    return cell;
}

#pragma mark - Table view Delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MPMediaItem *item = self.mediaItems[indexPath.row];
    self.musicPlayer.nowPlayingItem = item;
    [self.musicPlayer prepareToPlay];
    [self.musicPlayer play];
}

#pragma mark - MPMediaPickerControllerDelegate
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {
    self.mediaItems = mediaItemCollection.items;
    [self.musicPlayer setQueueWithItemCollection:mediaItemCollection];
    //这里采用延迟执行，解决_BSMachError这个错误
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
    });
    [mediaPicker dismissViewControllerAnimated:YES completion:NULL];
}

- (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker {
    NSLog(@"mediaPickerDidCancel");
    [mediaPicker dismissViewControllerAnimated:YES completion:NULL];
}

@end
